import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';
import 'package:largefriends/events/user_relation_event.dart';
import 'package:largefriends/service/manager/lf_event_helper.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/widgets/check_hidden.dart';

toManageAccess() {
  Get.toNamed(LFRoute.pathManageAccess);
}

class LfManageAccessLogic extends LfBaseListController<LfUserEntity> {
  LfAuthService authService = Get.find();
  @override
  Future<IndicatorResult> getCustomParamHttpRequest({bool isRefresh = false}) {
    return httpRequest(httpUrl: apiWhoRequestPrivateAlbum, refresh: isRefresh);
  }

  @override
  getCustomRequestParam({bool isRefresh = false}) {
    requestParam['status'] = '[1,2]';
  }

  accessPrivate(LfUserEntity user) {
    checkHidden(() {
      showLoading();
      LfDio().post<LfBoolEntity>(
        url: apiAcceptPrivatePhoto,
        params: {'userId': user.userId},
      ).then((value) {
        hideLoading();
        for (var element in dataList) {
          if (element.userId == user.userId) {
            element.canAccessMyPrivate = 1;
            update();
            LfEventHelper().getEvent().fire(UserRelationEvent(
                uid: user.userId!, type: UserRelationType.requestAccept));
          }
        }
      }, onError: (error) {
        hideLoading();
        if (error is NetError) {
          showSnackBar(error.msg);
        }
      });
    });
  }

  revokePrivate(LfUserEntity user) {
    showLoading();
    LfDio().post<LfBoolEntity>(
      url: apiUnSharePrivatePhoto,
      params: {'userId': user.userId},
    ).then((value) {
      hideLoading();
      dataList.removeWhere((element) => element.userId == user.userId);
      update();
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }

  declinedPrivate(LfUserEntity entity) {
    showLoading();
    LfDio.instance.post<LfBoolEntity>(
      url: apiRejectPrivatePhoto,
      params: {'userId': entity.userId},
    ).then((value) {
      hideLoading();
      dataList.removeWhere((element) => element.userId == entity.userId!);
      update();
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }
}
